package com.ruoyi.repayment.controller;


import com.ruoyi.common.core.utils.bean.BeanUtils;
import com.ruoyi.repayment.cofig.AverageCapitalUtils;
import com.ruoyi.repayment.mapper.LoanMapper;
import com.ruoyi.repayment.pojo.Product;
import com.ruoyi.repayment.pojo.Repayment;
import com.ruoyi.repayment.pojo.RepaymentDb;
import com.ruoyi.repayment.pojo.RepaymentPeriod;
import com.ruoyi.repayment.utils.RepaymentUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/loan/sq")
public class RepaymentController {
    @Autowired
    LoanMapper loanMapper;


    @PostMapping("/tryRepayment/{productId}/{userId}/{repaymentId}")
    public Repayment add(
            @PathVariable Integer productId,
            @PathVariable Integer userId,
            @PathVariable Integer repaymentId,
            @RequestBody RepaymentDb repaymentDb
    ) {
        Product list=loanMapper.findByList(productId);
        Integer totalmonth=list.getPeriods();
        double loansprice=list.getMaxquota();
        double yearRate=list.getInterestRate();
        Long loanRepaymentWay = repaymentDb.getLoanRepaymentWay();
        Repayment repayment = new Repayment();
        BeanUtils.copyProperties(repaymentDb, repayment);
        if (repaymentId == 2) {//等额本金
            //等额本金的每月偿还本金
            double perMonthPrincipal = AverageCapitalUtils.getPerMonthPrincipal(loansprice, totalmonth);
            //等额本金的每月偿还利息
            Map<Integer, Double> perMonthInterest = AverageCapitalUtils.getPerMonthInterest(loansprice, yearRate, totalmonth);
            //等额本金的每月偿还本金和利息
            Map<Integer, Double> perMonthPrincipalInterest = AverageCapitalUtils.getPerMonthPrincipalInterest(loansprice, yearRate,totalmonth);
            //等额本金的总利息
            double interestCount = AverageCapitalUtils.getInterestCount(loansprice, yearRate, totalmonth);
            List<RepaymentPeriod> repaymentPeriodList = new ArrayList<>();
            for (int i = 1; i <= totalmonth; i++) {
                RepaymentPeriod repaymentPeriod = new RepaymentPeriod();
                repaymentPeriod.setNo(i);
                repaymentPeriod.setPrinl(perMonthPrincipal);
                repaymentPeriod.setIntere(perMonthInterest.get(i));
                repaymentPeriod.setPrincipalAnd(perMonthPrincipalInterest.get(i));
                repaymentPeriodList.add(repaymentPeriod);
            }
            repayment.setRepaymentList(repaymentPeriodList);
            repayment.setTotalInterest(interestCount);
            return repayment;
        }
        if (repaymentId == 1) {//等额本息
            //等额本息的每月偿还利息
            Map<Integer, BigDecimal> perMonthInterest = RepaymentUtil.getPerMonthInterest(loansprice, yearRate, totalmonth);
            //额本息的每月偿还本金
            Map<Integer, BigDecimal> perMonthPrincipal = RepaymentUtil.getPerMonthPrincipal(loansprice, yearRate, totalmonth);
            //等额本息的总利息
            double interestCount = RepaymentUtil.getInterestCount(loansprice, yearRate, totalmonth);
            //等额本息计算获取还款方式为等额本息的每月偿还本金和利息
            double perMonthPrincipalInterest = RepaymentUtil.getPerMonthPrincipalInterest(loansprice, yearRate, totalmonth);
            List<RepaymentPeriod> repaymentPeriodList = new ArrayList<>();
            for (int i = 1; i <= totalmonth; i++) {
                RepaymentPeriod repaymentPeriod = new RepaymentPeriod();
                repaymentPeriod.setNo(i);
                repaymentPeriod.setIntere(perMonthInterest.get(i).doubleValue());
                repaymentPeriod.setPrinl(perMonthPrincipal.get(i).doubleValue());
                repaymentPeriod.setPrincipalAnd(perMonthPrincipalInterest);
                repaymentPeriodList.add(repaymentPeriod);
            }
            repayment.setRepaymentList(repaymentPeriodList);
            repayment.setTotalInterest(interestCount);
            return repayment;

        }
        return repayment;
    }

}
